package com.zzc.mcpclient.web;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.reader.TextReader;
import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.redis.RedisVectorStore;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class RagFileController {
    private final RedisVectorStore redisVectorStore;
    // 上传文件
    @PostMapping("/upload")
    public Object upload(@RequestParam("file") MultipartFile file) {
        Resource resource = file.getResource();
        TikaDocumentReader tikaDocumentReader = new TikaDocumentReader(resource);
        List<Document> documents = tikaDocumentReader.get();
        TokenTextSplitter tokenTextSplitter = new TokenTextSplitter();
        List<Document> list = tokenTextSplitter.apply(documents);
        redisVectorStore.doAdd(list);
        return "success";
    }
}
